<template>
  <div class="app-container">
    <!--行内的表单 进行条件查询-->
    <el-form :inline="true" ref="ruleForm" :model="tbUserVo" class="demo-form-inline">
      <el-row>
        <el-col :span="6">
          <el-form-item label="部门:" prop="departmentId">
            <el-select style="width: 220px;" v-model="tbUserVo.departmentId" placeholder="请选择">
              <el-option
                v-for="item in deptList"
                :label="item.name"
                :value="item.id">
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">
                <span class="el-tag el-tag--success el-tag--mini el-tag--plain">{{ item.deptCount }}人</span>
              </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户名:" prop="username">
            <el-input style="width: 220px;" v-model="tbUserVo.username" placeholder="用户名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="邮箱:" prop="email">
            <el-input style="width: 220px;" v-model="tbUserVo.email" placeholder="邮箱"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="性别:" prop="sex">
            <el-radio v-model="tbUserVo.sex" :label="0">男</el-radio>
            <el-radio v-model="tbUserVo.sex" :label="1">女</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="昵称:" prop="nickname">
            <el-input style="width: 220px;" v-model="tbUserVo.nickname" placeholder="昵称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="18">
          <el-form-item>
            <el-button icon="el-icon-refresh" @click="resetForm('ruleForm')">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="userLists">查询</el-button>
            <el-button type="success" icon="el-icon-plus" @click="hreftwo('ruleForm')">新增</el-button>
            <el-button icon="el-icon-download"@click="exportUser">导出</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--表格区域-->
    <el-table
      :data="userList"
      border
      style="width : 100%">
      <el-table-column
        prop="id"
        label="#"
        width="50">
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户名"
        width="100">
      </el-table-column>
      <el-table-column
        prop="sex"
        label="性别"
        width="50">
        <template slot-scope="scope">
          {{scope.row.sex==0?'男':(scope.row.sex==1?'女':'保密')}}
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="所属部门"
        sortable
        width="120">
      </el-table-column>
      <el-table-column
        prop="birth"
        label="生日"
        sortable
        width="180">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="180">
      </el-table-column>
      <el-table-column
        prop="phoneNumber"
        label="电话"
        width="120">
      </el-table-column>
      <el-table-column
        prop="status"
        label="是否禁用"
        width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status==1"></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="180">
        <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
        <el-button type="danger" size="mini" icon="el-icon-delete"></el-button>
        <el-button type="warning" size="mini" icon="el-icon-s-tools"></el-button>
      </el-table-column>
    </el-table>
    <!--分页区域-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[6, 10, 20, 30]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>

</template>

<script>
  import {getUserList,exportUserslist} from '@/api/user'
  import {getDeptList} from '@/api/dept'

  export default {
    name: "UserList",
    data() {
      return {
        //用户集合
        userList: [],
        //部门集合
        deptList: [],
        //部门名称
        deptName: '',
        current: 1,
        size: 6,
        total: 0,
        tbUserVo: {
          username: '',
          email: '',
          sex: '',
          nickname: '',
          departmentId: ''
        },
      }
    },
    created() {
      this.userLists();
      this.deptLists();

    },
    methods: {
      userLists() {
        getUserList(this.current, this.size, this.tbUserVo).then(res => {
          this.userList = res.data.records
          this.total = res.data.total;
          console.log(res.data)

        }).catch(err => {
          console.log("查询时发生错误")
        })
      },
      //窗体加载的时候也需要查询部门信息
      deptLists() {
        getDeptList().then(res => {
          this.deptList = res.data.deptAndCount
        }).catch(err => {

        })
      },
      exportUser(){
        exportUserslist(this.tbUserVo).then(res => {
          //这里没有返回数据,但是要在响应头中进行下载
          /*将二进制文件转化成为可访问url*/
          let url = window.URL.createObjectURL(new Blob([res]));
          var a = document.createElement("a");
          document.body.appendChild(a);
          a.href = url;
          a.download = "用户列表.xls";
          a.click();
          window.URL.revokeObjectURL(url);
        }).catch(err => {

        })
      },
      handleSizeChange(val) {
        this.size = val;
        this.userLists();
      },
      handleCurrentChange(val) {
        this.current = val;
        this.userLists();
      },
      //重置表单
      resetForm(formName) {
        this.tbUserVo = {}
        this.$refs[formName].resetFields();
      },
      hreftwo() {
        this.$router.push({path: '/add'})
      },
    }
  }
</script>

<style scoped>

</style>
